import GlobalEvent from "../../../cfw/event/GlobalEvent";
import { EventName } from "../../../public/Config";
import GrowItemModel from "../model/GrowItemModel";
import Player from "../Player";

const { ccclass, property } = cc._decorator;

@ccclass
export default class EggView extends cc.Component {

    @property(cc.Node)
    egg: cc.Node = null;

    @property(cc.Node)
    shell: cc.Node = null;
    protected model: Player;
    start() {
        this.model = Player.instance();
        GlobalEvent.instance().on(EventName.CHANGE_LEVEL, this.changeLevel, this)
        GlobalEvent.instance().on(EventName.BROKEN_SHELL, this.brokenShell, this)
        this.changeLevel()
    }

    changeLevel() {
        let growItem: GrowItemModel = this.model.getGrowItemModel();
        if (!growItem) {
            cc.error(' growITem is null ')
            return;
        }
        if (growItem.getFront() < 0) {
            this.egg.active = false
        } else {
            this.egg.active = true;
        }
    }

    onDestroy() {
        GlobalEvent.instance().off(EventName.CHANGE_LEVEL, this.changeLevel, this)
        GlobalEvent.instance().off(EventName.BROKEN_SHELL, this.brokenShell, this)
    }

    brokenShell() {
        this.shell.active = false;
    }
    // update (dt) {}
}
